package com.example.bysj.Service;
//201902104061徐中堂
import com.example.bysj.Dao.*;
import com.example.bysj.domain.User;
import com.example.bysj.domain.authority.Actor;
import com.example.bysj.domain.authority.Role;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
//声明一个业务处理类
@Service
public class UserService {
    //自动注入
    @Autowired
    private UserDao userDao;
    @Autowired
    private RolePermissionDao rolePermissionDao;
    @Autowired
    private UserAssRoleDao userAssRoleDao;
    @Autowired
    private ResourceDao resourceDao;
    @Autowired
    private ActorDao actorDao;

    /**
     * 根据用户名获得用户对象
     * 用户对象储存角色集合
     * 每个角色储存权限集合
     * @param name
     * @return
     */
    public User getByUserName(String name){
        User user = userDao.getByUserName(name);
        List<Role> roles = null;
        if (user==null){
            return null;
        }else {
            roles = userAssRoleDao.getRoleByUserId(user.getId());
            for (int i = 0; i<roles.size(); i++){
                //根据roleId在连接表中查找，把查找到的权限集合赋值给对应的角色对象
                roles.get(i).setResources(rolePermissionDao.getResourceByRoleId(roles.get(i).getId()));
            }
        }
        //给user设置角色对象
        user.setRoles(roles);
        return user;
    }
    //查找全部
    public List<User> findAll(){
        List<User> users = userDao.findAll();
        return users;
    }
    //添加用户
    public boolean addUser(User user) {
        boolean add = userDao.add(user);

        return add;
    }
    //登录

    public Actor login(User user){
        return userDao.login(user);
    }
}

